package com.woniuxy.microcurrentserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.bean.current.PageInfos;
import com.woniuxy.microcurrentserver.entity.*;
import com.woniuxy.microcurrentserver.service.CurrentFundInfoService;
import com.woniuxy.microcurrentserver.util.FailEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (CurrentFundInfo)表控制层
 *
 * @author makejava
 * @since 2021-04-08 14:40:02
 */
@Slf4j
@Api("活期基金详情")
@RefreshScope
@RestController
@RequestMapping(value = "currentFundInfo",produces = MediaType.APPLICATION_JSON_VALUE)
public class CurrentFundInfoController {
    /**
     * 服务对象
     */
    @Resource
    private CurrentFundInfoService currentFundInfoService;

    /**
     * 通过code查询单条数据
     *
     * @param pageInfos code,id
     * @return 单条数据
     */
    @PostMapping("selectOne")
    @ApiOperation("查询单条活期基金详情")
    public ResultBean selectOne(@RequestBody PageInfos pageInfos) {
        //查询该条数据
        CurrentFundInfo currentFundInfo = this.currentFundInfoService.queryByCode(pageInfos.getCode(),pageInfos.getId());
        //判断是否查询成功
        if (currentFundInfo == null) {
            return ResultBean.builder().msg(FailEnum.CURRENT_QUERY_ERROR.getDesc()).code(FailEnum.CURRENT_QUERY_ERROR.getCode()).build();
        }
        //查询历史数据
        List<CurrentFundHistory> list = this.currentFundInfoService.queryHistory(currentFundInfo.getCode());

        List<Series> thirtyDaySeries = new ArrayList<>();
        List<Series> ninetyDaySeries = new ArrayList<>();
        List<Series> halfYearSeries = new ArrayList<>();
        List<Series> yearSeries = new ArrayList<>();

        String[] thirtyDayDate = new String[30];
        String[] thirtyDayIncome = new String[30];
        String[] ninetyDayDate = new String[90];
        String[] ninetyDayIncome = new String[90];
        String[] halfYearDate = new String[180];
        String[] halfYearIncome = new String[180];
        String[] yearDate = new String[list.size()];
        String[] yearIncome = new String[list.size()];


        //封装数据
        for (int i = 0; i < list.size(); i++) {
            yearDate[i] = list.get(i).getUpdate();
            yearIncome[i] = list.get(i).getSevenDaysYearIncome();
        }
        //封装数据
        for (int j = 0; j < 180; j++) {
            halfYearDate[j] = list.get(j).getUpdate();
            halfYearIncome[j] = list.get(j).getSevenDaysYearIncome();
        }
        //封装数据
        for (int k = 0; k < 90; k++) {
            ninetyDayDate[k] = list.get(k).getUpdate();
            ninetyDayIncome[k] = list.get(k).getSevenDaysYearIncome();
        }
        //封装数据
        for (int l = 0; l < 30; l++) {
            thirtyDayDate[l] = list.get(l).getUpdate();
            thirtyDayIncome[l] = list.get(l).getSevenDaysYearIncome();
        }


        thirtyDaySeries.add(Series.builder().data(thirtyDayIncome).name("近三十天七日年化利率曲线").build());
        ninetyDaySeries.add(Series.builder().data(ninetyDayIncome).name("近三月七日年化利率曲线").build());
        halfYearSeries.add(Series.builder().data(halfYearIncome).name("近半年七日年化利率曲线").build());
        yearSeries.add(Series.builder().data(yearIncome).name("近一年七日年化利率曲线").build());

        List<LineData> lineDatas = new ArrayList<>();
        lineDatas.add(LineData.builder().categories(thirtyDayDate).series(thirtyDaySeries).build());
        lineDatas.add(LineData.builder().categories(ninetyDayDate).series(ninetyDaySeries).build());
        lineDatas.add(LineData.builder().categories(halfYearDate).series(halfYearSeries).build());
        lineDatas.add(LineData.builder().categories(yearDate).series(yearSeries).build());

        ResponseData responseData = new ResponseData().setCurrentFundInfo(currentFundInfo).setLineData(lineDatas);


        return ResultBean.builder().msg(FailEnum.SUCCESS.getDesc()).code(FailEnum.SUCCESS.getCode()).data(responseData).build();
    }



    /**
     * 查询多条数据
     *
     * @return 多条数据
     */
    @GetMapping("selectAll")
    @ApiOperation("分页查询所有基金详情")
    public ResultBean selectAll(@ApiParam("页码page") Integer page, @ApiParam("模糊搜索参数name") String name) {
        PageInfo<CurrentFundInfo> currentFundInfoPageInfo = this.currentFundInfoService.queryAllByLimit(page, name);
        //判断是否查询成功
        if (currentFundInfoPageInfo==null){
            return ResultBean.builder().msg(FailEnum.CURRENT_QUERY_ERROR.getDesc()).code(FailEnum.CURRENT_QUERY_ERROR.getCode()).build();
        }
        return ResultBean.builder().msg(FailEnum.SUCCESS.getDesc()).code(FailEnum.SUCCESS.getCode()).data(currentFundInfoPageInfo).build();
    }

    /**
     * class:
     * create by: 陈酒
     * description:新增一条数据
     * create time: 2021/4/15
     *
     * @param: currentFundInfo
     */
    @PostMapping("insertOne")
    public ResultBean insertOne(CurrentFundInfo currentFundInfo) {
        boolean i = this.currentFundInfoService.insert(currentFundInfo);
        //判断是否新增成功
        if (i) {
            return ResultBean.builder().msg(FailEnum.SUCCESS.getDesc()).code(FailEnum.SUCCESS.getCode()).build();
        }
        return ResultBean.builder().msg(FailEnum.INSERT_ERROR.getDesc()).code(FailEnum.INSERT_ERROR.getCode()).build();
    }

    /**
     * class:
     * create by: 陈酒
     * description:软删除一条数据
     * create time: 2021/4/15
     *
     * @param: code , status
     */
    @PostMapping("delete")
    public ResultBean delete(@RequestBody PageInfos pageInfos) {

//        System.out.println(pageInfos.getCodes() + "::" + pageInfos.getStatus());
        boolean i = this.currentFundInfoService.delete(pageInfos);
        //判断是否删除成功
        if (i) {
            return ResultBean.builder().msg(FailEnum.SUCCESS.getDesc()).code(FailEnum.SUCCESS.getCode()).build();
        }
        return ResultBean.builder().msg(FailEnum.UPDATE_ERROR.getDesc()).code(FailEnum.UPDATE_ERROR.getCode()).build();
    }

    /**
     * class:
     * create by: 陈酒
     * description:修改一条数据
     * create time: 2021/4/15
     *
     * @param:currentFundInfo
     */
    @PostMapping("update")
    public ResultBean update(CurrentFundInfo currentFundInfo) {
        boolean i = this.currentFundInfoService.update(currentFundInfo);
        //判断是否修改成功
        if (i) {
            return ResultBean.builder().msg("修改成功").code(200).build();
        }
        return ResultBean.builder().msg(FailEnum.UPDATE_ERROR.getDesc()).code(FailEnum.UPDATE_ERROR.getCode()).build();
    }

}
